package regex;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Objects;

/**
 * @fileName: TiebaRegex
 * @author: Han
 * @date: 2018/6/4 11:39
 * @dscription: 解析贴吧内容
 */
public class TiebaRegex {

    public static String parseContent(String pageContent) {

        StringBuilder discussSB = new StringBuilder();
        List<String> blocks = Regex.splitByRegex(pageContent, "l_post l_post_bright j_l_post clearfix  ");
        for (String block : blocks) {
            try {
                discussSB.append(parseBlock(block));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return discussSB.toString();
    }

    /**
     * 解析回复块中昵称,等级,回复,楼层,时间
     *
     * @param block 回复块
     * @return 各种信息
     */
    private static String parseBlock(String block) {

        String userName = parseName(block);
        String userRank = parseRank(block);
        String discuss = parseDiscuss(block);
        String[] tailInfo = parseTailInfo(block);
        String floor = tailInfo[0];
        String time = tailInfo[1];

        String info = "";
        if (!"null".equals(userName) && !"null".equals(userName) && !"null".equals(userName) && !"null".equals(userName) && !"null".equals(userName)) {
            info = "[(" + floor + ")" + userName + "(" + userRank + "级):" + discuss + "(" + time + ")]";
        }

        return info.replaceAll("<[\\S\\s]*?[^<>]>", "").replaceAll("\\s","");
    }

    /**
     * 解析尾部信息:楼层,时间
     *
     * @param block 回复块
     * @return 尾部信息
     */
    private static String[] parseTailInfo(String block) {

        String floor = Regex.matchString(block, "[0-9]{1,10}楼");
        String time = Regex.matchString(block, "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}");

        return new String[]{
                floor,
                time
        };

    }

    /**
     * 解析百度贴吧评论
     *
     * @param block 回复块
     * @return 回复
     */
    private static String parseDiscuss(String block) {
        // TODO: 2018/6/4 贴吧楼中楼回复是另外发送类似https://tieba.baidu.com/p/totalComment...样式的请求,里面的全部是Unicode的json数据
        // TODO: 2018/6/4 本次只抓取当前楼层的数据,不抓取楼中楼回复

        return Regex.matchString(block,"(?<=d_post_content j_d_post_content[\\s\\S]{0,100}?[^>]>)" +
                "[\\s\\S]*?[^</div>]{0}(?=</div>)");
    }

    /**
     * 解析出等级
     *
     * @param block 回复块
     * @return 用户等级
     */
    private static String parseRank(String block) {

        return Regex.matchString(block,"(?<=<div class=\"d_badge_lv\">)[\\d]{1,3}(?=</div>)");
    }

    /**
     * 解析当前用户昵称
     *
     * @param block 回复块
     * @return 用户昵称
     */
    private static String parseName(String block) {

        return Regex.matchString(block,"(?<=<li class=\"d_name\" data-field='\\{&quot;user_id&quot;:[\\d]{1,100}}'>)" +
                "[\\s\\S]*?[^</a>]{0}</a>");
    }

    public static int parsePagination(String pageContent){
        return Integer.parseInt(Regex.matchString(pageContent, "[\\d]{1,10}(?=</span>页</li>)"));
    }
}
